﻿var ns = namespace("kaskelot.sp.ui");

ns.persondetails = function () {

	function getPerson() {
		// Parametrar
		var id = getParameterByName("PersonId");

		$(".blockable-container").block({ message: '<h1>Var god vänta...</h1>' });

		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientPersonService.svc/GetPerson",
			type: "GET",
			data: { id: id },
			processData: true,
			cache: false,
			success:
                function (data) {
                	if (!data.GetPersonResult.HasError) {
                		$("#title-container").html(data.GetPersonResult.Data.Namn);

                		$("#grundinfo-container").html(
                            $("#grundinfo-template").render(data.GetPersonResult.Data)
                        );

                		$("#andradav-container").html(
                            $("#andradav-template").render(data.GetPersonResult.Data)
                        );

                		if (data.GetPersonResult.Data.Fritext != null) // Skriv inte över informationstexten som ligger i div:en
                		{
                			$("#fritext-container").html(
								$("#fritext-template").render(data.GetPersonResult.Data)
							);
                		}

                		$("#fritext-container").css("height", $("grundinfo-container").css("height"));
                	}
                	$(".blockable-container").unblock();
                },
			error:
                function (xhr) {
                	$(".blockable-container").unblock();
                	kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
                }
		});
	}

	function showEditForm() {
		var id = getParameterByName('PersonId');
		var selector = "#person-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "PersonForm",
				idString: id
			},
			selector: selector,
			openFunction: function () {
				$(".chzn-select-deselect").chosen({ disable_search: true });
			},
			okFunction: function () {
				$(selector).parent().block();

				var prog = kaskelot.sp.ui.formutils.getObject(selector);
				var data = "{\"form\":" + JSON.stringify(prog) + "}";

				var method = "";
				if (prog.Id === "")
					method = "CreatePerson";
				else
					method = "UpdatePerson";

				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientPersonService.svc/" + method,
					data: data,
					type: "POST",
					processData: false,
					contentType: "application/json",
					dataType: "json",
					success: function (resp) {
						if (resp[method + "Result"].HasError) {
							kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
						}
						else {

							$(selector).dialog("close");
							if (method === "UpdatePerson") {
								getPerson();
							}
							else if (method === "CreatePerson") {
								window.location = "./details.aspx?PersonId=" + resp[method + "Result"].Data;
							}
						}
					},
					error: function (xhr) {
						kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
					}
				});
			}
		});
	}

	return {
		getPerson: getPerson,
		showEditForm: showEditForm
	}
}();